<?php

/**
 * Klasa obsługi tablicy product
 *
 */
class ProductDAL extends DefaultDAL {

	protected static $objClassName = 'Product';
	protected static $objClassTable = 'product';
	protected static $objClassTablePK = 'id_product';
	private static $optClass;

	public static function Save(DalData $dalObj) {
		if ($dalObj->getObj()->GetId() == '-1') {
			return self::DefaultInsert($dalObj);
		} else {
			return self::DefaultUpdate($dalObj);
		}
	}

	public static function Insert(DalData $dalObj) {
		return self::DefaultInsert($dalObj);
	}

	public static function Update(DalData $dalObj) {
		return self::DefaultUpdate($dalObj);
	}

	public static function GetById(DalData $dalObj) {
		return self::DefaultGetById($dalObj);
	}

	public static function Delete(DalData $dalObj) {
		return self::DefaultDelete($dalObj);
	}

	public static function GetResult(DalData $dalObj) {
		return self::DefaultGetResult($dalObj);
	}

	public static function GetResultByLink($table, $id, $destination = false, $data = array(), $queryFields = array(), $limit = 0, $sortBy = null, $count = null) {
		$dalData = new DalData();
		$dalData->setObjClassName(self::GetObjClassName());
		$dalData->setObjClassTable(self::GetObjClassTable());
		$dalData->setObjClassTablePK(self::GetObjClassTablePK());
		$dalData->setOptClass(self::GetOptClass());
		$dalData->setCondition($data);
		$dalData->setQueryFields($queryFields);
		$dalData->setLimit($limit);
		$dalData->setSortBy($sortBy);
		$dalData->setCount($count);
		$dalData->setDataArray("mf_link_id", $id);
		$dalData->setDataArray("mf_link_table", $table);
		$dalData->setDataArray("mf_link_destination", $destination);
		return self::DefaultGetResultByLink($dalData);
	}

	public static function GetObjClassName() {
		if (self::$objClassName != '') {
			$class = self::$objClassName;
		} else {
			$class = str_replace('DAL', '', __CLASS__);
		}

		return $class;
	}

	public static function GetOptClass() {
		if (self::$optClass != null) {
			return self::$optClass;
		} else {
			return self::GetObjClassName();
		}
	}

	public static function GetObjClassTablePK() {
		if (self::$objClassTablePK != '') {
			$return = self::$objClassTablePK;
		} else {
			$class = self::GetObjClassName();
			$classObj = new $class();
			$return = $classObj->GetClassTablePK();
		}
		return $return;
	}

	public static function GetObjClassTable() {
		if (self::$objClassTable != '') {
			$return = self::$objClassTable;
		} else {
			$class = self::GetObjClassName();
			$classObj = new $class();
			$return = $classObj->GetTableName();
		}

		return $return;
	}

	/**
	 *
	 * @return DalData $dalData
	 */
	public static function GetDalDataObj($params = array()) {

		$dalData = new DalData($params);
		$dalData->setObjClassName(self::GetObjClassName());
		$dalData->setObjClassTable(self::GetObjClassTable());
		$dalData->setObjClassTablePK(self::GetObjClassTablePK());
		$dalData->setOptClass(self::GetOptClass());

		return $dalData;
	}

	public function GetBySlug($slug) {
		$res = self::GetResult(self::getDalDataobj(array('condition' => array('slug' => array($slug)))));
		if (empty($res))
			return null;

		return $res[0];
	}

	public function GenerateUniqueSlug($title, $current = null) {
		$counter = 0;
		$slug = Utils::TextToUrl($title);

		if($slug == $current)return $slug;

		while(!is_null(self::GetBySlug($slug))) {
			$slug = Utils::TextToUrl($title.'-'.++$counter);
		}

		return $slug;
	}
	
	
	

}

?>